package com.manage.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manage.platform.context.UserContext;
import com.manage.platform.dto.PositionDto;
import com.manage.platform.dto.query.PositionPageQuery;
import com.manage.platform.entity.PositionEntity;
import com.manage.platform.exception.CommonException;
import com.manage.platform.mapper.PositionMapper;
import com.manage.platform.utils.R;
import com.manage.platform.utils.RPage;
import com.manage.platform.vo.CommonVo;
import com.manage.platform.vo.PositionVo;
import com.manage.platform.vo.UserVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * YWD
 * 2025/9/24
 */
@AllArgsConstructor
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, PositionEntity> {

    private final UserServiceImpl userService;

    public RPage<List<PositionVo>> getPositionPage(PositionPageQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<PositionVo> list = this.baseMapper.getPositionPage(query);
        return RPage.ok(list, query.getPage(), query.getLimit(), page.getTotal());
    }

    public PositionVo getPositionDetails(Long id) {
        return this.baseMapper.getPositionDetails(id);
    }

    public Boolean savePosition(PositionDto dto) {
        //岗位是否存在
        LambdaQueryWrapper<PositionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PositionEntity::getIsDeleted, 0)
                .eq(PositionEntity::getName, dto.getName());
        List<PositionEntity> list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            throw new CommonException("该岗位已存在");
        }
        UserVo userVo = UserContext.getUser();
        dto.setUserId(userVo.getId());
        return save(BeanUtil.copyProperties(dto, PositionEntity.class));
    }

    public Boolean updatePosition(PositionDto dto) {
        //岗位是否存在
        LambdaQueryWrapper<PositionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PositionEntity::getIsDeleted, 0)
                .eq(PositionEntity::getName, dto.getName())
                .ge(PositionEntity::getId, dto.getId());
        List<PositionEntity> list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            throw new CommonException("该岗位已存在");
        }
        return updateById(BeanUtil.copyProperties(dto, PositionEntity.class));
    }

    public Boolean deletePosition(Long id) {
        Boolean b = userService.getByPositionId(id);
        if (b) {
            throw new CommonException("岗位正在使用,删除失败");
        }
        return removeById(id);
    }

    public List<CommonVo> getPositionList(String name) {
        return this.baseMapper.getPositionList(name);
    }
}
